package com.hfzy.ihk.web.freeswitchCall.job.freeswitch;

import com.hfzy.ihk.web.freeswitchCall.context.EventSocketInitBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @Auther: Administrator
 * @Date: 2018/12/7 11:43
 * @Description:
 */


@Component
@DependsOn("eventSocketInitBean")
public class FreeswitchJob {

    private static final Logger logger = LoggerFactory.getLogger(FreeswitchJob.class);

    @Autowired
    private EventSocketInitBean initClient;

    @Scheduled(cron = "0/15 * * * * ? ")
    @Async
    public void checkClinetStatus() {

//        logger.info("freeswitch log : 当前可用线路为"+EventSocketInitBean.getFreeLines().size()+",每15秒检测与freeswitch连接情况");

        boolean canSend = initClient.getClient().canSend();

        if (!canSend) {
            logger.info("freeswitchlog : 与freeswitch的链接已经中断");

            logger.info("freeswitch log : 正在进行与freeswitch的重新连接");
            //initClient.getClient().connect(initClient.getHost(),initClient.getPort(),initClient.getPassword(),10000);
            initClient.initClient();
            logger.info("freeswitch log : 与freeswitch重新连接成功!!");

        } else {

            logger.info("freeswitchlog : 与freeswitch的链接正常");
        }
    }


    /**
     * 每天凌晨4点清空
     */
    @Scheduled(cron = "1 1 4 * * ?")
    @Async
    public void clearCallMap(){

        logger.info("freeswitchlog : clearCallMap,size:{}",EventSocketInitBean.getLocalConnection().size());
        initClient.clearCallMap();
    }
}
